#ifdef GL_FRAGMENT_PRECISION_HIGH
    precision highp float;
    precision highp int;
#else
    precision mediump float;
    precision mediump int;
#endif

#include "Lighting.glsl";
uniform vec4 u_DiffuseColor;
#if defined(COLOR)&&defined(ENABLEVERTEXCOLOR)
    varying vec4 v_Color;
#endif
#ifdef ALPHATEST
    uniform float u_AlphaTestValue;
#endif
#ifdef DIFFUSEMAP
    uniform sampler2D u_DiffuseTexture;
    //添加图层
    uniform sampler2D u_BaseTexture;
    uniform sampler2D u_texture1;
    uniform sampler2D u_texture2;
    uniform sampler2D u_mask1;
    uniform sampler2D u_mask2;
    uniform int u_maskIndex;
    uniform float u_alpha;
    uniform int u_alphaIndex;
#endif

#if defined(DIFFUSEMAP)||((defined(DIRECTIONLIGHT)||defined(POINTLIGHT)||defined(SPOTLIGHT))&&(defined(SPECULARMAP)||defined(NORMALMAP)))
    varying vec2 v_Texcoord0;
#endif
#ifdef LIGHTMAP
    varying vec2 v_LightMapUV;
    uniform sampler2D u_LightMap;
#endif

varying vec3 v_Normal;
#if defined(DIRECTIONLIGHT)||defined(POINTLIGHT)||defined(SPOTLIGHT)
    varying vec3 v_ViewDir; 
    uniform vec3 u_MaterialSpecular;
    uniform float u_Shininess;
    #ifdef LEGACYSINGLELIGHTING
        #ifdef DIRECTIONLIGHT
            uniform DirectionLight u_DirectionLight;
        #endif
        #ifdef POINTLIGHT
            uniform PointLight u_PointLight;
        #endif
        #ifdef SPOTLIGHT
            uniform SpotLight u_SpotLight;
        #endif
    #else
        uniform mat4 u_View;
        uniform vec4 u_ProjectionParams;
        uniform vec4 u_Viewport;
        uniform int u_DirationLightCount;
        uniform sampler2D u_LightBuffer;
        uniform sampler2D u_LightClusterBuffer;
    #endif
    #ifdef SPECULARMAP 
        uniform sampler2D u_SpecularTexture;
    #endif
#endif

#ifdef NORMALMAP 
    uniform sampler2D u_NormalTexture;
    varying vec3 v_Tangent;
    varying vec3 v_Binormal;
#endif
#ifdef FOG
    uniform float u_FogStart;
    uniform float u_FogRange;
    uniform vec3 u_FogColor;
#endif
#if defined(POINTLIGHT)||defined(SPOTLIGHT)||defined(RECEIVESHADOW)
    varying vec3 v_PositionWorld;
#endif

#include "GlobalIllumination.glsl";//\"GlobalIllumination.glsl use uniform should at front of this
#include "ShadowHelper.glsl"

varying float v_posViewZ;
#ifdef RECEIVESHADOW
    #if defined(SHADOWMAP_PSSM2)||defined(SHADOWMAP_PSSM3) 
        uniform mat4 u_lightShadowVP[4];
    #endif
    #ifdef SHADOWMAP_PSSM1
        varying vec4 v_lightMVPPos;
    #endif
#endif

void main_castShadow()
{
    //gl_FragColor=vec4(v_posViewZ,0.0,0.0,1.0);
    gl_FragColor=packDepth(v_posViewZ);
    #if defined(DIFFUSEMAP)&&defined(ALPHATEST)
        float alpha = texture2D(u_DiffuseTexture,v_Texcoord0).w;
        if( alpha < u_AlphaTestValue )
        {
            discard;
        }
    #endif
}

void main_normal()
{
    float alpha = 0.8;//添加属性
    vec3 normal;//light and SH maybe use normal
    #if defined(NORMALMAP)
        vec3 normalMapSample = texture2D(u_NormalTexture, v_Texcoord0).rgb;
        normal = normalize(NormalSampleToWorldSpace(normalMapSample, v_Normal, v_Tangent,v_Binormal));
    #else
        normal = normalize(v_Normal);
    #endif
    #if defined(DIRECTIONLIGHT)||defined(POINTLIGHT)||defined(SPOTLIGHT)
        vec3 viewDir= normalize(v_ViewDir);
    #endif
    LayaGIInput giInput;
    #ifdef LIGHTMAP
        giInput.lightmapUV=v_LightMapUV;
    #endif
    vec3 globalDiffuse=layaGIBase(giInput,1.0,normal);
    vec4 mainColor=u_DiffuseColor;
    #ifdef DIFFUSEMAP
        vec4 difTexColor=texture2D(u_DiffuseTexture, v_Texcoord0);
        //处理遮罩
        vec4 maskColor1 = texture2D(u_mask1, v_Texcoord0);
        vec4 maskColor2 = texture2D(u_mask2, v_Texcoord0);
        if(maskColor1.x >= 0.1 || maskColor1.y >= 0.5 || maskColor1.z > 0.5 ){
            vec4 texture1Color = texture2D(u_texture1, v_Texcoord0);
            difTexColor.xyz = texture1Color.xyz;
            // difTexColor.w = maskColor1.w;
            // difTexColor = texture1Color;
            // alpha = difTexColor.w;
        }
        if(maskColor2.x >= 0.1 || maskColor2.y >= 0.5 || maskColor2.z > 0.5 ){
            vec4 texture2Color = texture2D(u_texture2, v_Texcoord0);
            if(texture2Color.w >= 0.5){
                difTexColor.xyz = texture2Color.xyz;
                // difTexColor.w = maskColor2.w;
                // difTexColor = texture2Color;
                // alpha = maskColor2.w;
            }
        }

        mainColor=mainColor*difTexColor;
        // mainColor.w = 0.8;
    #endif 
    #if defined(COLOR)&&defined(ENABLEVERTEXCOLOR)
        mainColor=mainColor*v_Color;
    #endif 
    #ifdef ALPHATEST
        if(mainColor.a<u_AlphaTestValue)
            discard;
    #endif
    vec3 diffuse = vec3(0.0);
    vec3 specular= vec3(0.0);
    #if defined(DIRECTIONLIGHT)||defined(POINTLIGHT)||defined(SPOTLIGHT)
        vec3 dif,spe;
        #ifdef SPECULARMAP
            vec3 gloss=texture2D(u_SpecularTexture, v_Texcoord0).rgb;
        #else
            #ifdef DIFFUSEMAP
                vec3 gloss=vec3(difTexColor.a);
            #else
                vec3 gloss=vec3(1.0);
            #endif
        #endif
    #endif
    #ifdef LEGACYSINGLELIGHTING
        #ifdef DIRECTIONLIGHT
            LayaAirBlinnPhongDiectionLight(u_MaterialSpecular,u_Shininess,normal,gloss,viewDir,u_DirectionLight,dif,spe);
            diffuse+=dif;
            specular+=spe;
        #endif
        #ifdef POINTLIGHT
            LayaAirBlinnPhongPointLight(v_PositionWorld,u_MaterialSpecular,u_Shininess,normal,gloss,viewDir,u_PointLight,dif,spe);
            diffuse+=dif;
            specular+=spe;
        #endif
        #ifdef SPOTLIGHT
            LayaAirBlinnPhongSpotLight(v_PositionWorld,u_MaterialSpecular,u_Shininess,normal,gloss,viewDir,u_SpotLight,dif,spe);
            diffuse+=dif;
            specular+=spe;
        #endif
    #else
        #ifdef DIRECTIONLIGHT
            for (int i = 0; i < MAX_LIGHT_COUNT; i++) 
            {
                if(i >= u_DirationLightCount)
                    break;
                DirectionLight directionLight = getDirectionLight(u_LightBuffer,i);
                LayaAirBlinnPhongDiectionLight(u_MaterialSpecular,u_Shininess,normal,gloss,viewDir,directionLight,dif,spe);
                diffuse+=dif;
                specular+=spe;
            }
        #endif
        #if defined(POINTLIGHT)||defined(SPOTLIGHT)
            ivec4 clusterInfo =getClusterInfo(u_LightClusterBuffer,u_View,u_Viewport, v_PositionWorld,gl_FragCoord,u_ProjectionParams);
            #ifdef POINTLIGHT
                for (int i = 0; i < MAX_LIGHT_COUNT; i++) 
                {
                    if(i >= clusterInfo.x)//PointLightCount
                        break;
                    PointLight pointLight = getPointLight(u_LightBuffer,u_LightClusterBuffer,clusterInfo,i);
                    LayaAirBlinnPhongPointLight(v_PositionWorld,u_MaterialSpecular,u_Shininess,normal,gloss,viewDir,pointLight,dif,spe);
                    diffuse+=dif;
                    specular+=spe;
                }
            #endif
            #ifdef SPOTLIGHT
                for (int i = 0; i < MAX_LIGHT_COUNT; i++) 
                {
                    if(i >= clusterInfo.y)//SpotLightCount
                        break;
                    SpotLight spotLight = getSpotLight(u_LightBuffer,u_LightClusterBuffer,clusterInfo,i);
                    LayaAirBlinnPhongSpotLight(v_PositionWorld,u_MaterialSpecular,u_Shininess,normal,gloss,viewDir,spotLight,dif,spe);
                    diffuse+=dif;
                    specular+=spe;
                }
            #endif
        #endif
    #endif
    #ifdef RECEIVESHADOW
        float shadowValue = 1.0;
        #ifdef SHADOWMAP_PSSM3
            shadowValue = getShadowPSSM3(u_shadowMap1,u_shadowMap2,u_shadowMap3,u_lightShadowVP,u_shadowPSSMDistance,u_shadowPCFoffset,v_PositionWorld,v_posViewZ,0.001);
        #endif
        #ifdef SHADOWMAP_PSSM2
            shadowValue = getShadowPSSM2(u_shadowMap1,u_shadowMap2,u_lightShadowVP,u_shadowPSSMDistance,u_shadowPCFoffset,v_PositionWorld,v_posViewZ,0.001);
        #endif
        #ifdef SHADOWMAP_PSSM1
            shadowValue = getShadowPSSM1(u_shadowMap1,v_lightMVPPos,u_shadowPSSMDistance,u_shadowPCFoffset,v_posViewZ,0.001);
        #endif
        gl_FragColor =vec4(mainColor.rgb*(globalDiffuse + diffuse*shadowValue),mainColor.a);
    #else
        // gl_FragColor =vec4(mainColor.rgb*(globalDiffuse + diffuse),mainColor.a);
        //修改alpha
        gl_FragColor =vec4(mainColor.rgb*(globalDiffuse + diffuse),mainColor.a);
    #endif
    #if defined(DIRECTIONLIGHT)||defined(POINTLIGHT)||defined(SPOTLIGHT)
        #ifdef RECEIVESHADOW
            gl_FragColor.rgb+=specular*shadowValue;
        #else
            gl_FragColor.rgb+=specular;
        #endif
    #endif
    #ifdef FOG
        float lerpFact=clamp((1.0/gl_FragCoord.w-u_FogStart)/u_FogRange,0.0,1.0);
        gl_FragColor.rgb=mix(gl_FragColor.rgb,u_FogColor,lerpFact);
    #endif
}

void main()
{
    #ifdef CASTSHADOW
        main_castShadow();
    #else
        main_normal();
    #endif
}